#include #define PIXEL_PIN D9 #define NUM_PIXELS 30 #define HALL_PIN D8 // --- Thresholds (based on your calibration) --- #define ON_THRESHOLD 3800 #define OFF_THRESHOLD 3400 Adafruit_NeoPixel strip(NUM_PIXELS, PIXEL_PIN, NEO_GRB + NEO_KHZ800); // --- Hall sensor state --- float filtered = 0; bool hallState = false; // ---------- SETUP ---------- void setup() { Serial.begin(115200); strip.begin(); strip.setBrightness(50); strip.show(); // initialize filter baseline filtered = analogRead(HALL_PIN); Serial.println("System Ready"); } // ---------- HALL DIGITAL FUNCTION ---------- bool readHallDigital() { int raw = analogRead(HALL_PIN); // Low-pass filter (removes spikes) filtered = 0.8 * filtered + 0.2 * raw; // Hysteresis (stable switching) if (!hallState && filtered > ON_THRESHOLD) { hallState = true; } if (hallState && filtered < OFF_THRESHOLD) { hallState = false; } return hallState; } // ---------- LED CONTROL ---------- void setAllPixels(int r, int g, int b) { for (int i = 0; i < NUM_PIXELS; i++) { strip.setPixelColor(i, strip.Color(r, g, b)); } strip.show(); } // ---------- LOOP ---------- void loop() { static bool lastState = false; bool currentState = readHallDigital(); // Update LEDs only when state changes if (currentState != lastState) { if (currentState) { setAllPixels(0, 255, 0); // GREEN → magnet near Serial.println("MAGNET DETECTED"); } else { setAllPixels(255, 0, 0); // RED → magnet away Serial.println("NO MAGNET"); } lastState = currentState; } delay(20); }